/*******************************************************************************
 * Copyright (c) 2009 Kevin Bierhoff and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Kevin Bierhoff - initial API and implementation
 *******************************************************************************/
package com.googlecode.twelfdevtools.console;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.ui.console.FileLink;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.ui.console.IHyperlink;
import org.eclipse.ui.console.IPatternMatchListener;
import org.eclipse.ui.console.PatternMatchEvent;
import org.eclipse.ui.console.TextConsole;

import com.googlecode.twelfdevtools.builder.TwelfBuilder;


/**
 * Listens to Twelf error messages and creates links to source files
 * accordingly.
 * @author Kevin Bierhoff
 *
 */
public class TwelfConsoleErrorListener implements IPatternMatchListener {
	
	private TextConsole console;
	private IProject project;
	
	public TwelfConsoleErrorListener(IProject project) {
		this.project = project;
	}

	public int getCompilerFlags() {
		return TwelfErrorMessage.TWELF_ERROR.flags();
	}

	public String getLineQualifier() {
		return "Error:";
	}

	public String getPattern() {
		return TwelfErrorMessage.TWELF_ERROR.pattern();
	}

	public void connect(TextConsole console) {
		this.console = console;
	}

	public void disconnect() {
		this.console = null;
	}

	public void matchFound(PatternMatchEvent event) {
		try {
			int offset = event.getOffset();
			int length = event.getLength();
			TwelfErrorMessage e = TwelfErrorMessage.parseError(console.getDocument(), offset, length);
			if(e != null) {
				IFile file = getFile(e.getFileName());
				
				// report user problem
				reportProblem(file, e);
				
				// place link in console
				IHyperlink h = new FileLink(file, null, -1, -1, e.getStartRow());
				if(length > 7) length -= 7; // take off " Error:" at the end
				console.addHyperlink(h, offset, length);
			}
			else {
				// weird
			}
		}
		catch (BadLocationException e) {
			System.err.println("Could not place link");
			e.printStackTrace();
		}
		catch (NumberFormatException e) {
			System.err.println("Could not find line number for link");
			e.printStackTrace();
		}
	}

	private IFile getFile(String fileInProject) {
		return project.getFile(fileInProject.replace('\\', '/'));
	}
	
	private void reportProblem(IFile f, TwelfErrorMessage error) {
		if(error.isSummaryError())
			// do not create markers for summary errors
			return;
		try {
			IMarker m = f.createMarker(TwelfBuilder.PROBLEM_MARKER_TYPE);
			m.setAttribute(IMarker.LINE_NUMBER, error.getStartRow());
			m.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_NORMAL);
			m.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
			String msg = error.getDetails();
			if(msg.length() == 0) msg = "Unknown Twelf error, see console for details";
			m.setAttribute(IMarker.MESSAGE, msg);
			String location = error.getLocationString();
			if(location != null)
				m.setAttribute(IMarker.LOCATION, location);
		} 
		catch (CoreException e) {
			System.err.println("Error placing Twelf error marker");
			e.printStackTrace();
		}
	}

}
